﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

namespace ElectronicRecords.Models
{
    public class SpecialistsRepository
    {
        private ElectronicRecordsDataContext _datacontext;

         public SpecialistsRepository(ElectronicRecordsDataContext d)
        {
            _datacontext = d;
        }

        public IEnumerable<Specialists> GetSpecialists()
        {
            return _datacontext.Specialists.OrderBy(c => c.Firstname);
        }

        public Specialists GetSpecialist(int specialistId)
        {
            return _datacontext.Specialists.SingleOrDefault(c =>c.ID_Specialist ==specialistId);

        }

        public Specialists CreateSpecialist(string firstName, string lastName, string phone, string email)
        {
            Specialists s = new Specialists {Firstname=firstName, Lastname=lastName, Phone=phone, Email=email};
            _datacontext.Specialists.InsertOnSubmit(s);
            _datacontext.SubmitChanges();
            return s;
        }

        public void UpdateSpecialist(Specialists s)
        {
            Specialists dbSpecialist = GetSpecialist(s.ID_Specialist);
            dbSpecialist.Firstname = s.Firstname;
            dbSpecialist.Lastname = s.Lastname;
            dbSpecialist.Phone = s.Phone;
           // dbSpecialist.Procedure_Specialist_XREF = s.Procedure_Specialist_XREF;
          //  dbSpecialist.Shedules = s.Shedules;
            dbSpecialist.Email = s.Email;
          //  dbSpecialist.aspnet_Users = s.aspnet_Users;
          //  dbSpecialist.CreatedOn = c.CreatedOn;
            dbSpecialist.CreatedBy = s.CreatedBy;
            dbSpecialist.Email = s.Email;
            _datacontext.SubmitChanges();
        }

        public void DeleteSpecialist(int specialistId)
        {
            Specialists s=GetSpecialist(specialistId);
            
            _datacontext.Specialists.DeleteOnSubmit(s);
            _datacontext.SubmitChanges();
        }

        public Specialists CreateSpecialist(Specialists s)
        {
            //Specialists s = new Specialists { Firstname = firstName, Lastname = lastName, Phone = phone, Email = email };
            _datacontext.Specialists.InsertOnSubmit(s);
            _datacontext.SubmitChanges();
            return s;
        }

        public IEnumerable<Procedure_Specialist_XREF> GetServices(int id)
        {
            return GetSpecialist(id).Procedure_Specialist_XREF.OrderBy(pc => pc.ID_PROCEDURESPECIALIST);
        }

        public bool RemoveAllService(int id)
        {
            Specialists s = GetSpecialist(id);
            foreach (Procedure_Specialist_XREF ps in s.Procedure_Specialist_XREF)
            {
                _datacontext.Procedure_Specialist_XREF.DeleteOnSubmit(ps);
            }
            _datacontext.SubmitChanges();
            return true;
        }

        public bool AddService(Procedure_Specialist_XREF p)
        {
                
                
                _datacontext.Procedure_Specialist_XREF.InsertOnSubmit(p);
                _datacontext.SubmitChanges();
                return true;
        }

        public bool AddShedules(Shedules s)
        {
            _datacontext.Shedules.InsertOnSubmit(s);
            _datacontext.SubmitChanges();
            return true;
        }

        public bool RemoveShedules(Shedules s)
        {
            Shedules ss= _datacontext.Shedules.SingleOrDefault(c => c.ID_Shedules == s.ID_Shedules);
            _datacontext.Shedules.DeleteOnSubmit(ss);
            _datacontext.SubmitChanges();
            return true;
        }

        public IEnumerable<Shedules> GetAllShedules(int id)
        {
            return GetSpecialist(id).Shedules.OrderBy(pc=>pc.Date_End);
        }

        public IEnumerable<Shedules> GetShedulesBetweenDate(int id, DateTime begin, DateTime end)
        {
            return GetSpecialist(id).Shedules.Where(s => CompareBetweenDate(begin,end,s.Date_Begin)).AsEnumerable<Shedules>();
        }

        private bool CompareBetweenDate(DateTime time1, DateTime time2,DateTime timei)
        {
            
            if (DateTime.Compare(time1, timei) <= 0 && DateTime.Compare(time2, timei) >= 0)
                return true;
            else return false;
            
        }

        public Shedules GetShedule(int specialist_id,int id)
        {
            return GetSpecialist(specialist_id).Shedules.SingleOrDefault(c => c.ID_Shedules==id);
        }

        public bool UpdateShedule(Shedules s)
        {
            Shedules ss = GetShedule(s.ID_Specialist,s.ID_Shedules);
            ss.Date_Begin = s.Date_Begin;
            ss.Date_End = s.Date_End;
            ss.CreatedBy = s.CreatedBy;
            _datacontext.SubmitChanges();
            return true;
        }

    }
}